﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

public class Barrel
{
    protected Texture2D barrelTexture;
    protected Texture2D testCollider;

    protected Rectangle localBounds;
    /// <summary>
    /// Gets a rectangle which bounds this Object in world space.
    /// </summary>
    public Rectangle BoundingRectangle
    {
        get
        {
            return new Rectangle((int)Position.X, (int)Position.Y, barrelTexture.Width, barrelTexture.Height);
        }
    }

    public Vector2 Position;

    public Direction _Direction
    {
        get { return direction; }
        set { direction = value; }
    }
    private Direction direction;

    public bool Rolled
    {
        get { return rolled; }
        set 
        {
            rolled = value; 
        }
    }
    private bool rolled;

    public Barrel(Vector2 position)
    {
        Position = position;
    }

    private float rollingTime = 1500;

    public void LoadContent(ContentManager content)
    {
        barrelTexture = content.Load<Texture2D>("Textures/MapMock/barrel");
        testCollider = content.Load<Texture2D>("Textures/Characters/testCollider");
    }

    public void Update(GameTime gameTime)
    {
        if (rolled)
        {
            if (rollingTime > 0)
            {
                rollingTime -= gameTime.ElapsedGameTime.Milliseconds;
                switch (_Direction)
                {
                    case Direction.Up:
                        Position.Y -= rollingTime * 0.007f;
                        break;
                    case Direction.Down:
                        Position.Y += rollingTime * 0.007f;
                        break;
                    case Direction.Right:
                        Position.X += rollingTime * 0.0095f;
                        Position.Y += (1000 - rollingTime) * 0.005f;
                        break;
                    case Direction.Left:
                        Position.X -= rollingTime * 0.0095f;
                        Position.Y += (1000 - rollingTime) * 0.005f;
                        break;
                }
            }
            else
            {
                rolled = false;
                rollingTime = 1500;
                GameSession.BarrelsList.Remove(this);
            }
        }
    }

    /// <summary>
    /// Draws this Barrel.
    /// </summary>
    public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        //Test Bounding Size
        //spriteBatch.Draw(testCollider, new Vector2(BoundingRectangle.X, BoundingRectangle.Y + GameSession.screenAdjustment),
        //  new Rectangle(0, 0, BoundingRectangle.Width, BoundingRectangle.Height), Color.White);

        spriteBatch.Draw(barrelTexture, new Vector2(Position.X, Position.Y + GameSession.screenAdjustment), Color.White);
    }

    public void DoABarrelRoll(Direction direction)
    {
        Rolled = true;
        _Direction = direction;
    }
}
